package com.xnx3.wangmarket.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间值转换
 */
public class DateFormatUtils {

    /***
     * 字符串时间转换毫秒值
     * ****/
    public static Long dateGetTimeMs(String date) {
        long time = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            time = sdf.parse(date).getTime();
        } catch (ParseException e) {
            System.out.println("时间戳转换失败！");
        }
        return time;
    }

    /***
     * Long 类型毫秒值转换日期
     * ***/
    public static String dateFormatgetDate(Long ms) {

        Date date = new Date();
        date.setTime(ms);
        String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        return str;
    }
    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @param formatStr
     * @return
     */
    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }
    /**
     * 日期格式字符串转换成时间戳
     * @param date 字符串日期
     * @param format 如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String date_str,String format){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime()/1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}

